<?php

class Flights extends CI_Controller {

    function __construct() {
        parent::__construct();
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
        } else {
            redirect('login/costumer', 'refresh');
        }
        $this->load->model('costumer');
        $this->load->model('rute');
        $this->load->model('airline');
        $this->load->model('deposit');
        $this->load->helper('deposit_helper');
    }

    function index() {
        $id = $this->id();
        $data['id'] = $id;
        $data['rows'] = $this->costumer->index($id);
        $data['deposits'] = $this->deposit->index($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['content'] = 'costumers/index';
        $this->load->view('template/costumer', $data);
    }

    function info() {
        $id = $this->id();
        $data['id'] = $id;
        $data['rows'] = $this->costumer->index($id);
        $data['deposits'] = $this->deposit->index($id);
        $data['content'] = 'costumers/info';
        $this->load->view('template/costumer', $data);
    }

    function edit() {
        $id = $this->id();
        $data['id'] = $id;
        $data['rows'] = $this->costumer->index($id);
        $data['deposits'] = $this->deposit->index($id);
        $data['content'] = 'costumers/edit';
        $this->load->view('template/costumer', $data);
    }
    
    function update(){
        $this->load->library('form_validation');
        $name = $this->form_validation->set_rules('name', 'Name', 'required');
        $username = $this->form_validation->set_rules('username', 'Usernmae', 'required|min_length[5]|max_length[12]|is_unique[users.username]');
        $email = $this->form_validation->set_rules('email', 'email', 'required|valid_email|is_unique[users.email]');
        $mobile_phone = $this->form_validation->set_rules('mobile_phone', 'Mobile phone', 'required');

        if ($this->form_validation->run() == FALSE) {
            $data['content'] = 'costumers/edit';
            $this->load->view('template/costumer', $data);
        } else {
            $id = $this->id();
            $name = $this->input->post('name', TRUE);
            $username = $this->input->post('username', TRUE);
            $email = $this->input->post('email', TRUE);
            $last_name = $this->input->post('last_name', TRUE);
            $address = $this->input->post('address', TRUE);
            $home_phone = $this->input->post('home_phone', TRUE);
            $mobile_phone = $this->input->post('mobile_phone', TRUE);
            $country = $this->input->post('country', TRUE);
            $city = $this->input->post('city', TRUE);
            $state = $this->input->post('state', TRUE);
            $zip_code = $this->input->post('zip_code', TRUE);
            $date = date('Y-m-d');
            $this->costumer->update($id, $name, $username, $email, $last_name, $address, $home_phone, $mobile_phone, $country, $city, $state, $zip_code, $date);
        }
    }

    function jadwal() {
        $data['content'] = 'costumers/jadwal';
        $this->load->view('template/costumer', $data);
    }

    function id() {
        $session_data = $this->session->userdata('logged_in');
        return $session_data['id'];
    }

}

?>
